package com.mark.org.controller;

import com.mark.basic.util.AjaxResult;
import com.mark.basic.util.BusinessException;
import com.mark.basic.util.ExcelUtil;
import com.mark.basic.util.PageList;
import com.mark.org.domain.Shop;
import com.mark.org.domain.ShopAuditLog;
import com.mark.org.query.ShopQuery;
import com.mark.org.service.IShopService;
import com.mark.org.vo.ShopVo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/shop")
@Api(tags = "店铺接口",description = "店铺接口详细描述")
public class ShopController {
    @Autowired
    private IShopService shopService;
    @PostMapping("/settlement")
    public AjaxResult settlement (@RequestBody Shop shop){
        try {
            shopService.settlement(shop);
            return AjaxResult.me();
        }catch (BusinessException e){
            return AjaxResult.me().setMassage("系统繁忙,请稍后操作！！！"+ e.getMessage()).setSuccess(false);
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMassage("系统繁忙,请稍后操作！！！").setSuccess(false);
        }
    }
    @GetMapping("/{id}")
    /**
     * 查询单个数据
     */
    public Shop queryById(@PathVariable("id") Long id){
        return shopService.listById(id);
    }
    @GetMapping
    /**
     * 查询所有数据
     */
    public List<Shop> queryAll(){
        return shopService.listAll();
    }
    @DeleteMapping("/{id}")
    /**
     * 删除数据
     */
    public AjaxResult del(@PathVariable("id") Long id){
        try {
            shopService.del(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMassage("系统繁忙,请稍后操作！！！").setSuccess(false);
        }
    }
    @PutMapping
    /**
     * 添加或修改数据
     */
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        System.out.println(shop.getId());
        if (shop.getId() == null) {
            shopService.add(shop);
        } else {
            shopService.update(shop);
        }
        return AjaxResult.me();
    }
    /**
     * 分页查询
     */
    @PostMapping
    public PageList<Shop> queryPage(@RequestBody ShopQuery query){
        PageList<Shop> aa = shopService.queryPage(query);
        return aa;
    }

    /**
     * 批量删除
     */
    @PatchMapping
    public AjaxResult batchRemove(@RequestBody List<Long> ids){
        try {
            System.err.println(ids);
            shopService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMassage("系统繁忙,请稍后操作！！！").setSuccess(false);
        }
    }

    /**
     * 审核通过
     * @return
     */
    @PostMapping("/audit/pass")
    public AjaxResult pass(@RequestBody ShopAuditLog shopAuditLog){
        System.out.println(shopAuditLog);
        try {
            shopService.pass(shopAuditLog);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMassage("系统繁忙,请稍后操作！！！").setSuccess(false);
        }

    }
    /**
     * 审核驳回
     * @return
     */
    @PostMapping("/audit/reject")
    public AjaxResult reject(@RequestBody ShopAuditLog shopAuditLog){
        try {
            shopService.reject(shopAuditLog);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMassage("系统繁忙,请稍后操作！！！").setSuccess(false);
        }
    }
    /**
     * 导出数据export
     */
    @GetMapping("/export")
    public void export( HttpServletResponse response){
        try {
            List<Shop> shops = shopService.listAll();
            // (List<Map<String, Object>> list, String fileName, HttpServletResponse response)
            ExcelUtil.exportExcel(shops,null, "店铺信息", Shop.class, "shop.xlsx", response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 导入数据export
     */
    @PostMapping("/import/Excel")
    public void importExcel (@RequestPart("file") MultipartFile file){
        //String file = "F:\\文件名.xls";
        //解析excel，
        List<Shop> list = ExcelUtil.importExcel(file,1,1,Shop.class);
        //也可以使用MultipartFile,使用 FileUtil.importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass)导入
        shopService.batchSave(list);
        System.out.println("导入数据一共"+list.size()+"行");
        //保存到数据库中[自己实现，提示：动态sql 批量插入]

    }

    /**
     * 获取报表数据
     */
    @GetMapping("/echarts")
    public Map<String, Object> echarts(){
        // 获取报表的数据
        List<ShopVo> shopVos = shopService.queryByState();
        // 将x、y的数据分别取出
        List<Integer> states = new ArrayList<>();
        List<Integer> counts = new ArrayList<>();
        // 遍历集合
        for (ShopVo shopVo : shopVos) {
            Integer state1 = shopVo.getState();
            states.add(state1);
            counts.add(shopVo.getCountNum());
        }
        // 创建一个map集合  将xy对应装入
        HashMap<String, Object> map = new HashMap<>();
        map.put("x",states);
        map.put("y",counts);
        return map;
    }
}